package isg3.data;

import isg3.domain.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Date;
import java.util.*;


import isg3.utils.UIDGenerator;

public class JDBCEventoDAO implements IEventoDAO {
	
	private Connection conn;
	
	public JDBCEventoDAO(){
		conn = ConnectionManager.getInstance().checkOut();
		System.out.println("SE HA CREADO LA CONEXION");
	}

	public void insert(Evento p) {
	
        String oido = UIDGenerator.getInstance().getKey();
        PreparedStatement stmt = null;
        String sql = "INSERT INTO eventos ( OIDEvento, titulo, descripcion, estado,fechaEven) VALUES (?, ?, ?, ?, ?) ";
        try {
            stmt = conn.prepareStatement(sql);

            stmt.setString(1, oido);
            stmt.setString(2, p.getTituloEvento());
            stmt.setString(3, p.getDescripcionEvento());
            stmt.setString(4, p.getEstadoEvento());
            stmt.setString(5, "");
    

            stmt.executeUpdate();

            //insert details
           // ddao.insert(conn, o.getDetails(), oido);

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}

	public Evento select(String name) {
		// TODO Auto-generated method stub
		PreparedStatement stmt = null;
        ResultSet result = null;
        Evento even = null;
        String sql = "SELECT * FROM eventos WHERE (titulo = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(2, name);
            result = stmt.executeQuery();

            result.next();
            even = new Evento();
            even.setTituloEvento(result.getString("titulo"));
            even.setDescripcionEvento(result.getString("descripcion"));
            even.setEstadoEvento(result.getString("estado"));
            even.setFecha(result.getDate("fechaEven"));
            even.setIdEvento(result.getString("OIDEvento"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return even;
    }


	public List selectAllEventos() {
		// TODO Auto-generated method stub
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM eventos"; //WHERE (tipo= tipoU)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            //stmt.setString(4, tipo);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Evento temp = new Evento();
                temp.setTituloEvento(result.getString("titulo"));
                temp.setDescripcionEvento(result.getString("descripcion"));
                temp.setEstadoEvento(result.getString("estado"));
                temp.setFecha(result.getDate("fechaEven"));
                temp.setIdEvento(result.getString("OIDEvento"));
        
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }		
        	return searchResults;
	}
	public void updateEstado(Evento p, String est) {
		
        PreparedStatement stmt = null;
        String sql = "UPDATE eventos SET estado = ? WHERE titulo = ?";
        try {
            stmt = conn.prepareStatement(sql);
            
            stmt.setString(1, est);
            stmt.setString(2, p.getTituloEvento());
            
            stmt.executeUpdate();

            //insert details
           // ddao.insert(conn, o.getDetails(), oido);

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}
	
	public void updateEvento(String idE, java.util.Date f){
		PreparedStatement stmt = null;
        String sql = "UPDATE eventos SET fechaEven = ? WHERE OIDEvento = ?";
        try {
            stmt = conn.prepareStatement(sql);
            java.util.Date myDate= f ;
            stmt.setDate(1, new java.sql.Date(myDate.getTime()));
            stmt.setString(2, idE);
            
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
        	ConnectionManager.getInstance().checkIn(conn);
        	System.out.println("SE HA CERRADO LA CONEXION");
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
	}
}

